/*
 * @name accountSettings.js

 * @copyright (c) 2011. You are free to use this class.
 * 				  Modify it the way you want it.
 * 				  Please let me know about any modifications done.
 *
 */
var UserAccount = {

    /* Function init
     * This function is the entry point of the Object.
     * @param void
     * @return void
     * @access public
     * */
    init:function () {

        /* Handle account settings action */
        /*$("a#account-setting").bind('click',function(event){
         event.preventDefault(); //Preventing browser default functionalities
         $.colorbox({
         inline:true,
         scrolling:false,
         overlayClose:false,
         escKey:false,
         top:'10%',
         title:'Account Information',
         href:"#account-form-window",
         });
         });*/

        /* Handle the change password checkbox */
        /* $('#reset-pass-check').click(function(){
         // check its status and display the change password box.
         if($('#reset-password-holder').is(':visible')) {
         $.validationEngine.closePrompt('.formError',true);
         $("#old_password").val('');
         $("#password_1").val('');
         $("#password_2").val('');
         $('#reset-password-holder').hide();
         } else {
         $('#reset-password-holder').show();
         }
         });*/

        $('#save-btn').click(function () {
            UserAccount.saveSettings();
        });

        $('#cancel-btn').click(function () {
            document.getElementById('account-form').reset();
            $.validationEngine.closePrompt('.formError', true);
        });

        $('#account-form').validationEngine();

        // Switches mode
        $('#reset-pass-check').change(function () {
            if (this.checked) {
                $('#reset-password-holder').show();
            }
            else {
                $("#old_password").val('');
                $("#password_1").val('');
                $("#password_2").val('');
                $('#reset-password-holder').hide();
            }
        });
    },

    /* Function used to save the data sent by the user
     * @name saveData
     * @param formId
     * @return void
     * @access public
     **/
    saveSettings:function () {
        /* Validate the form*/
        var validationStatus = $('#account-form').validationEngine({returnIsValid:true});
        console.log(validationStatus)
        /* When the Validation Status is true meaning that the data input are correct then the data can be processed. */
        if (validationStatus) {
            /*If the checkbox is checked, then all passwords fields must not be empty*/

            if ($('#reset-pass-check').attr('checked')) {//Validate and Build promt
                if ($("#old_password").val() == "" || $("#password_1").val() == "" || $("#password_2").val() == "") {
                    $.validationEngine.buildPrompt("#reset-password-prompt", "All passwords fields are required", "error");
                    return false;
                }
            }

            jLib.showFormLoading();

            /* Preparation of the data to send to the server to be processed. Here we retrieve the data from the login form. */
            var query = $('#account-form').serialize();
            var url = $("#update-url").val();
            /* Send the data to the server and handle the server response */
            $.post(url, query, function (response) {
                var txt = '';
                if (typeof response.mesg == 'object') {
                    for (megTxt in response.mesg) {
                        txt += response.mesg[megTxt] + '<br />';
                    }
                }
                else {
                    txt = response.mesg
                }
                jLib.hideFormLoading();
                if (response.code === 0) {
                    //document.getElementById('account-form').reset();
                    $.validationEngine.closePrompt('.formError', true);

                    $('#reset-pass-check').attr('checked', '');
                    $('#reset-password-holder').hide();

                    jLib.message(txt,'success');

                    /*$.modal({
                        contentBg:false,
                        contentAlign:'center',
                        content:txt,
                        resizable:false,
                        actions:{},
                        buttons:{
                            'Continue':{
                                classes:'green-gradient',
                                click:function (modal) {
                                    modal.closeModal();
                                }
                            },
                            'Logout to Relogin':{
                                classes:'grey-gradient',
                                click:function (modal) {
                                    modal.getModalContentBlock().message('Loging out...', { append:false, classes:['grey-gradient'] });
                                    window.location = $("#logout-url").val();
                                }
                            }
                        },
                        buttonsAlign:'center'
                    });*/
                }
                /* When there are Errors */
                else {
                    $.modal({
                        contentBg:false,
                        contentAlign:'center',
                        content:"<p class='message red-gradient small-margin-bottom'></span></span>"+txt+"</p>",
                        resizable:false,
                        actions:{},
                        buttons:{
                            'Close':{
                                classes:'red-gradient',
                                click:function (modal) {
                                    modal.closeModal();
                                }
                            }
                        },
                        buttonsAlign:'center'
                    });
                    //$.validationEngine.buildPrompt(".account-settings-prompt",txt,"error");
                    //jLib.setFeedback(response.mesg, 'error_message', false);
                }
            }, 'json');
        }
    },


    /* This function creates a modal window dialog
     * @param string entity
     * @return void
     * @access public
     * */
    createDialog:function () {
        // dialog properties
        var dialogTitle = null;
        var dialogButtons = {
            'save changes':function () {
                $thisObj.saveSettings();
            },
            'close':function () {
                $.validationEngine.closePrompt('.formError', true)
                $('#account-form-window').dialog('close');
            }
        };

        dialogTitle = 'User Account Details';
        // render the dialog box
        $('#account-form-window').dialog({
            modal:true,
            resizable:false,
            title:dialogTitle,
            width:400,
            buttons:dialogButtons,
            open:function (event, ui) {
                $(".ui-dialog-titlebar-close").hide();
            }

        });
    }

};

/* when the page is loaded */
$(document).ready(function () {
    //Hiding the loader bar
    UserAccount.init();
});

